package com.itbaizhan.mapper2;

import com.itbaizhan.pojo.Classes;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

//注解式开发
public interface ClassMapper2 {
     /*一对一关联查询  学生表与班级表(从表)
     * 一个学生对应一个班级
     学生表与班级表相关联的列是id*/
    @Select("select * from classes where cid = #{cid}")
     Classes findById(int cid);


    //一对多查询 一个班级对应的所有学生
    //查询所有班级
    @Select("select * from classes")
    //自定义映射关系
    @Results(id = "classMapper" , value = {
            @Result(id = true,property = "cid",column = "cid"),
            @Result(property = "className",column = "className"),
            /*映射的学生
             * many:表示该属性是一个集合
             * 一对多是懒加载*/
            @Result(property = "studentList",column = "cid",
                    many = @Many(select = "com.itbaizhan.mapper2.StudentMapper2.findById",
                    fetchType = FetchType.LAZY))
    })
    List<Classes> findAll();
}
